<template>
	<view>
		<view style="margin: 40upx 0;">
			<progress :percent="percent" stroke-width="10"></progress>
		</view>
		<view style="margin: 40upx 0;">
			<button type="primary" :loading="loading" :disabled="disabled" @click="upload">选择照片</button>
		</view>
		<view style="margin: 40upx 0;">
			<button type="primary" :loading="loading" :disabled="disabled" @click="getNews">获取新闻动态</button>
		</view>
	</view>

</template>
<script>
	var _self;
	export default {
		data(){
			return{
				percent: 0,
				loading: false,
				disabled: false
			}
		},
		methods: {
			upload: function() {
				_self = this;
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						const tempFilePaths = res.tempFilePaths;
						const uploadTask = uni.uploadFile({
							url: 'http://xn--15t076b.com/qxkj-test/test/test', 
							filePath: tempFilePaths[0],
							name: 'file',
							formData: { 
								'user': 'test'
							},
							success: function(uploadFileRes) {
								console.log(uploadFileRes);
								console.log(uploadFileRes.data);
							}
						});
						uploadTask.onProgressUpdate(function(res) {
							_self.percent = res.progress;
							console.log('上传进度' + res.progress);
							console.log('已经上传的数据长度' + res.totalBytesSent);
							console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
						});
					},
					error: function(e) {
						console.log(e);
					}
				});
			},
			getNews(){
				uni.request({
					url: "http://xn--15t076b.com/news/insert", //仅为示例，并非真实接口地址。
					method: "post",
					success: (res) => {
						console.log(res);
					}
				});
				
			}
		},
		onLoad: function() {

		}
	}
</script>
